<?php

/**
 * home actions.
 *
 * @package    webnews
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class homeActions extends sfActions {
    public function executeIndex(sfWebRequest $request) {

	$this->pager = new sfDoctrinePager(
		'Article',
		sfConfig::get('app_max_articles_on_page'));

	$this->pager->setQuery(Doctrine::getTable('Article')->getArticlesQuery());
	$this->pager->setPage($request->getParameter('page', 1));
	$this->pager->init();

    }

    public function executeNew(sfWebRequest $request) {
	$this->form = new ArticleForm();

    }

    public function executeCreate(sfWebRequest $request) {
	$this->forward404Unless($request->isMethod(sfRequest::POST));

	$this->form = new ArticleForm();

	$this->form->getObject()->setAuthor($this->getUser());

	$this->processForm($request, $this->form);

	$this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
	$this->forward404Unless($article = Doctrine::getTable('Article')->find(array($request->getParameter('id'))), sprintf('Object article does not exist (%s).', $request->getParameter('id')));
	$this->form = new ArticleForm($article);
    }

    public function executeAbout(sfWebRequest $request) {

    }

    public function executeUpdate(sfWebRequest $request) {
	$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
	$this->forward404Unless($article = Doctrine::getTable('Article')->find(array($request->getParameter('id'))), sprintf('Object article does not exist (%s).', $request->getParameter('id')));
	$this->form = new ArticleForm($article);

	$this->processForm($request, $this->form);

	$this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
	$request->checkCSRFProtection();

	$this->forward404Unless($article = Doctrine::getTable('Article')->find(array($request->getParameter('id'))), sprintf('Object article does not exist (%s).', $request->getParameter('id')));
	$article->delete();

	$this->redirect('home/index');
    }

    public function executeVotePositive(sfWebRequest $request) {
	$this->getResponse()->setContentType('text/html');
	$article = Doctrine::getTable('Article')->getArticle($request->getParameter('article_id'));
	if (Doctrine::getTable('PositiveVote')->hasVoted($this->getUser(),$request->getParameter('article_id'))==false) {
	    $article->vote_positive();
	}
	return $this->renderText(count($article->getPositiveVotes()));
    }

    public function executeVoteNegative(sfWebRequest $request) {
	$this->getResponse()->setContentType('text/html');
	$article = Doctrine::getTable('Article')->getArticle($request->getParameter('article_id'));
	if (Doctrine::getTable('NegativeVote')->hasVoted($this->getUser(),$request->getParameter('article_id'))==false) {
	    $article->vote_negative();
	}
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
	$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
	if ($form->isValid()) {
	    $form->save();
	    $this->redirect('home/index');
	}
    }

}
